package com.moji.controller.api;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.moji.common.base.ApiResult;
import com.moji.common.utils.HttpUtils;
import com.moji.entity.MjProduct;
import com.moji.entity.MjProductPurchase;
import com.moji.entity.vo.MijiMoYaoVo1;
import com.moji.entity.vo.MojiMoYaoVo3;
import com.moji.service.MjProductPurchaseService;
import com.moji.service.MjProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 产品表(MjProduct)表控制层
 *
 * @author makejava
 * @since 2024-07-21 14:36:36
 */
@RestController
@RequestMapping("api/mjProduct")
@Api(tags = "产品列表相关api接口", value = "产品列表相关api接口")
public class MjProductController {
    @Resource
    private MjProductPurchaseService mjProductPurchaseService;
    /**
     * 服务对象
     */
    @Resource
    private MjProductService mjProductService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation(value = "获取全部产品列表,不需要传参数")
    public ApiResult<List<MjProduct>> queryByPage() {

        return ApiResult.ok(this.mjProductService.queryByPage());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail")
    @ApiOperation(value = "根据id查看产品", response = MjProduct.class)
    public ApiResult<MjProduct> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjProductService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mjProduct 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加产品", response = MjProduct.class)
    public ApiResult<MjProduct> add(@Validated @RequestBody MjProduct mjProduct) {
        MjProduct mjProduct1 = this.mjProductService.insert(mjProduct);
        if (mjProduct1 == null) {
            return ApiResult.failed("此router已存在");
        }
        return ApiResult.ok(mjProduct1);
    }

    /**
     * 编辑数据
     *
     * @param mjProduct 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改产品", response = MjProduct.class)
    public ApiResult<MjProduct> edit(@Validated @RequestBody MjProduct mjProduct) {
        if (this.mjProductService.update(mjProduct) == null) {
            return ApiResult.failed("此router已存在");
        }
        return ApiResult.ok(this.mjProductService.update(mjProduct));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除产品", response = Boolean.class)
    public ApiResult<Boolean> deleteById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjProductService.deleteById(id));
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/purchase")
    @ApiOperation(value = "同步产品选购选项")
    public ApiResult<Boolean> purchase() {
        String s = HttpUtils.doGet4("http://demo.mojicb.com/tob-data-misc/eshop/access/enums");
        Gson gson1 = new Gson();
        MijiMoYaoVo1 mijiMoYaoVo1 = gson1.fromJson(s, MijiMoYaoVo1.class);
        if (mijiMoYaoVo1 != null && mijiMoYaoVo1.getCode() == 200) {
            Gson gson2 = new Gson();
            MojiMoYaoVo3 mijiMoYaoVo2 = gson2.fromJson(s, MojiMoYaoVo3.class);
            MojiMoYaoVo3.DataBean data = mijiMoYaoVo2.getData();
            Object endpoints = data.getEndpoints();
            JSONObject jsonObject = new JSONObject(endpoints);
            HashMap<String, String> hashMap = new HashMap<>();
            // 遍历JSONObject中的键值对
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                hashMap.put(key, value.toString());
                if(key.startsWith("GC27W"))
                {
                    LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                    queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20240819211006");
                    if (mjProductPurchaseService.getOne(queryWrapper) == null) {
                        MjProductPurchase mjProductPurchase = new MjProductPurchase();
                        mjProductPurchase.setCode("CPXG-20240819211006");
                        mjProductPurchase.setTitle("全球27W城市");
                        mjProductPurchase.setSubTitleCode(key);
                        mjProductPurchase.setOldSubTitle(value.toString());
                        mjProductPurchase.setCreateTime(new Date());
                        mjProductPurchase.setStatus(0);
                        mjProductPurchaseService.save(mjProductPurchase);
                    }
                }else if(key.startsWith("GHC"))
                {
                    LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                    queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20240819210900");
                    if (mjProductPurchaseService.getOne(queryWrapper) == null) {
                        MjProductPurchase mjProductPurchase = new MjProductPurchase();
                        mjProductPurchase.setCode("CPXG-20240819210900");
                        mjProductPurchase.setTitle("国内+国外热门城市天气类型");
                        mjProductPurchase.setSubTitleCode(key);
                        mjProductPurchase.setOldSubTitle(value.toString());
                        mjProductPurchase.setCreateTime(new Date());
                        mjProductPurchase.setStatus(0);
                        mjProductPurchaseService.save(mjProductPurchase);
                    }
                }

            }
            Object regions = data.getRegions();
            JSONObject jsonObject1 = new JSONObject(regions);
            // 遍历JSONObject中的键值对
            for (String key : jsonObject1.keySet()) {
                Object value = jsonObject1.get(key);
                hashMap.put(key, value.toString());
                LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20240819205017");
                if (mjProductPurchaseService.getOne(queryWrapper) == null) {
                    MjProductPurchase mjProductPurchase = new MjProductPurchase();
                    mjProductPurchase.setCode("CPXG-20240819205017");
                    mjProductPurchase.setTitle("城市筛选");
                    mjProductPurchase.setSubTitleCode(key);
                    mjProductPurchase.setOldSubTitle(value.toString());
                    mjProductPurchase.setCreateTime(new Date());
                    mjProductPurchase.setStatus(0);
                    mjProductPurchaseService.save(mjProductPurchase);
                }
            }
            Object rates = data.getRates();
            JSONObject jsonObject2 = new JSONObject(rates);

            // 遍历JSONObject中的键值对
            for (String key : jsonObject2.keySet()) {
                Object value = jsonObject2.get(key);
                hashMap.put(key, value.toString());
                LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20240819211039");
                if (mjProductPurchaseService.getOne(queryWrapper) == null) {

                    MjProductPurchase mjProductPurchase = new MjProductPurchase();
                    mjProductPurchase.setCode("CPXG-20240819211039");
                    mjProductPurchase.setTitle("选择调用量梯度");
                    mjProductPurchase.setSubTitleCode(key);
                    mjProductPurchase.setOldSubTitle(value.toString());
                    mjProductPurchase.setCreateTime(new Date());
                    mjProductPurchase.setStatus(0);
                    mjProductPurchaseService.save(mjProductPurchase);
                }
            }
            Object accessTypes = data.getAccessTypes();
            JSONObject jsonObject3 = new JSONObject(accessTypes);

            // 遍历JSONObject中的键值对
            for (String key : jsonObject3.keySet()) {
                Object value = jsonObject3.get(key);
                hashMap.put(key, value.toString());
                LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20240819210744");
                if (mjProductPurchaseService.getOne(queryWrapper) == null) {

                    MjProductPurchase mjProductPurchase = new MjProductPurchase();
                    mjProductPurchase.setCode("CPXG-20240819210744");
                    mjProductPurchase.setTitle("选择调用方式");
                    mjProductPurchase.setSubTitleCode(key);
                    mjProductPurchase.setOldSubTitle(value.toString());
                    mjProductPurchase.setCreateTime(new Date());
                    mjProductPurchase.setStatus(0);
                    mjProductPurchaseService.save(mjProductPurchase);
                }
            }

            Object durations = data.getDurations();
            JSONObject jsonObject4 = new JSONObject(durations);

            // 遍历JSONObject中的键值对
            for (String key : jsonObject4.keySet()) {
                Object value = jsonObject4.get(key);
                hashMap.put(key, value.toString());
                LambdaQueryWrapper<MjProductPurchase> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProductPurchase::getSubTitleCode, key);
                queryWrapper.eq(MjProductPurchase::getCode, "CPXG-20241023145123");
                if (mjProductPurchaseService.getOne(queryWrapper) == null) {

                    MjProductPurchase mjProductPurchase = new MjProductPurchase();
                    mjProductPurchase.setCode("CPXG-20241023145123");
                    mjProductPurchase.setTitle("服务时长");
                    mjProductPurchase.setSubTitleCode(key);
                    mjProductPurchase.setOldSubTitle(value.toString());
                    mjProductPurchase.setCreateTime(new Date());
                    mjProductPurchase.setStatus(0);
                    mjProductPurchaseService.save(mjProductPurchase);
                }
            }
            LambdaQueryWrapper<MjProductPurchase> queryWrapperProductPurchase = new LambdaQueryWrapper<>();
            List<MjProductPurchase> list = mjProductPurchaseService.list(queryWrapperProductPurchase);
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    MjProductPurchase mjProductPurchase = list.get(i);
                    if (hashMap.containsKey(mjProductPurchase.getSubTitleCode())) {
                        mjProductPurchase.setOldSubTitle(hashMap.get(mjProductPurchase.getSubTitleCode()));
                        mjProductPurchaseService.updateById(mjProductPurchase);
                    } else {
                        mjProductPurchaseService.removeById(mjProductPurchase.getId());
                    }
                }
            }
        }
        return ApiResult.ok(Boolean.TRUE);
    }
}

